package org.strieber.fca;

import java.util.ArrayList;
import java.util.List;

public class Rule {

	protected List<String> extents = new ArrayList<String>(); 
	protected List<String> intents = new ArrayList<String>();
	
	protected double support;
	protected double confidence;
	
	public Rule() {
		
	}
	
	public Rule(List<String> extents, List<String> intents, double support, double confidence) {
		this.extents = extents;
		this.intents = intents;
		this.support = support;
		this.confidence = confidence;
	}
	
	public void setExtents(List<String> extents) {
		this.extents = extents;
	}
	public void addExtent(String extent) {
		extents.add(extent);
	}
	public List<String> getExtents() {
		return extents;
	}
	public void clearExtents() {
		extents = new ArrayList<String>(); 
	}
	public void setIntents(List<String> intents) {
		this.intents = intents;
	}
	public void addIntent(String intent) {
		intents.add(intent);
	}
	public List<String> getIntents() {
		return intents;
	}
	public void clearIntents() {
		extents = new ArrayList<String>(); 
	}
	public boolean hasExtent(String extent) {
		for (int i = 0; i < extents.size(); i++) {
			String currentExtent = extents.get(i);
			if (currentExtent.equals(extent)) {
				return true;
			}
		}
		return false;
	}
	public boolean hasIntent(String intent) {
		for (int i = 0; i < intents.size(); i++) {
			String currentIntent = intents.get(i);
			if (currentIntent.equals(intent)) {
				return true;
			}
		}
		return false;
	}
	public boolean matchesExtent(String extent) {
		for (int i = 0; i < extents.size(); i++) {
			String currentExtent = extents.get(i);
			if (currentExtent.matches(extent)) {
				return true;
			}
		}
		return false;
	}
	public boolean matchesIntent(String intent) {
		for (int i = 0; i < intents.size(); i++) {
			String currentIntent = intents.get(i);
			if (currentIntent.matches(intent)) {
				return true;
			}
		}
		return false;
	}
	public boolean removeExactExtents(String extent) {
		boolean matched = false;
		for (int i = extents.size(); i > 0; i--) {
			String currentExtent = extents.get(i);
			if (currentExtent.equals(extent)) {
				extents.remove(i);
				matched = true;
			}
		}
		return matched;
	}
	
	public boolean removeExactIntents(String intent) {
		boolean matched = false;
		for (int i = intents.size(); i > 0; i--) {
			String currentIntent = intents.get(i);
			if (currentIntent.equals(intent)) {
				intents.remove(i);
				matched = true;
			}
		}
		return matched;
	}
	
	public boolean removeMatchedExtents(String extent) {
		boolean matched = false;
		// System.out.println("rme / extent: " + extent);
		for (int i = extents.size() - 1; i >= 0; i--) {
			String currentExtent = extents.get(i);
			// System.out.println("currentExtent: " + currentExtent);
			if (currentExtent.contains(extent)) {
				extents.remove(i);
				matched = true;
				// System.out.println("matched!");
			}
		}
		return matched;
	}
	
	public boolean removeMatchedIntents(String intent) {
		boolean matched = false;
		for (int i = intents.size(); i > 0; i--) {
			String currentIntent = intents.get(i);
			if (currentIntent.contains(intent)) {
				intents.remove(i);
				matched = true;
			}
		}
		return matched;
	}
	
	public void setSupport(double support) {
		this.support = support;
	}
	public double getSupport() {
		return support;
	}
	public void setConfidence(double confidence) {
		this.confidence = confidence;
	}
	public double getConfidence() {
		return confidence;
	}
	public String toString() {
		return "rule, e: " + extents + ", i: " + intents + ", S: "
			+ support + "%, C: " + confidence + "%";
	}
	public String cleanToString() {
		return extents + " => " + intents;
	}
}